﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;

namespace WebSite.Blogs
{
    public partial class BlogList : PreBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadContent(false);
            }
        }

        #region 加载内容
        private void LoadContent(bool mark)
        {
            //设置控件属性，从BasePage中得到
            PageConfig(BlogUPager);

            //List<Member> list = new List<Member();
            BlogHelper bhelper = new BlogHelper();
            Criteria c = new Criteria();
            Users uinfo=UserBase.GetCurrentUser();
            c.Add(CriteriaType.NotEquals, "Flag", 9);
            c.Add(CriteriaType.NotEquals, "Flag", -1);
            c.Add(CriteriaType.Equals, "UsersID", uinfo.ID);

            string title = txtTitle.Text.Trim();
            if (!string.IsNullOrEmpty(title))
            {
                c.Add(CriteriaType.Like, "Title", "%" + title + "%");
            }

            BlogUPager.ItemCount = bhelper.QueryBlogCountBySearch(c);

            // 以下的值从BasePage中得到
            BlogUPager.UrlFormat = UrlFormat;
            //BlogUPager.PrefixText = GetPreFixText(BlogUPager);

            if (mark)
            {
                PagerInit(BlogUPager);
            }

            string[] fields = new string[] { "ID", "BlogBigType", "BlogTypeName", "BlogTypeID", "Title", "BlogContent", "UsersID", "PVCount", "ReplyCount", "RecommandCount", "Flag", "HasPic", "CreateDate" };

            DataTable dt = new DataTable();
            dt = bhelper.QueryBlogBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Desc) }, BlogUPager.Begin - 1, BlogUPager.Count, fields);

            dt.Columns.Add(new DataColumn("strFlag"));
            dt.Columns.Add(new DataColumn("strHasPic"));

            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    string flag = dr["Flag"] + "";
                    string strflag = "";
                    if (flag == "0")
                    {
                        strflag = "正常显示";
                    }
                    else if (flag == "-9")
                    {
                        strflag = "被系统屏蔽";
                    }
                    dr["strFlag"] = strflag;

                    //判断是否存在图片
                    string haspic = dr["haspic"] + "";
                    if (haspic == "1")
                    {
                        dr["strHasPic"] = "<img src=\"../images/smallpic.jpg\" alt=\"此创作包含图片\"/>";
                    }
                    else
                    {
                        dr["strHasPic"] = "";
                    }
                }
                rptBlog.Visible = true;
                rptBlog.DataSource = dt;
                rptBlog.DataBind();
                lblMessage.Visible = false;
            }
            else
            {
                rptBlog.Visible = false;
                lblMessage.Visible = true;
                if (string.IsNullOrEmpty(title))
                {
                    lblMessage.Text = "您还没有创作，<a href=\"BlogAdd.aspx\"><font color=\"red\">马上开始创作</font></a>";
                }
                else
                {
                    lblMessage.Text = "没有符合条件的创作结果";
                }

            }

        }
        #endregion

        #region 列表事件
        protected void rptBlog_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");
            string id = hidID.Value;

            if (e.CommandName == "delete")
            {
                Blog binfo = new Blog();
                BlogHelper bhelper = new BlogHelper();
                binfo = bhelper.GetBlog(id);
                binfo.ID = id;
                binfo.Flag = 9;
                bhelper.EditBlog(binfo);
                //更新用户表中的blognum
                Users uinfo = UserBase.GetCurrentUser();
                int blognum = uinfo.BlogNum;
                blognum = blognum - 1;
                uinfo.BlogNum = blognum;
                UsersHelper uhelper = new UsersHelper();
                uhelper.EditUsers(uinfo);

                Tools.Alert("删除成功！");
            }
            LoadContent(false);
        }
        #endregion

        #region 搜索
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            SetCondition();
            LoadContent(true);
        }
        #endregion

        #region 查询条件设置
        private void SetCondition()
        {
            string title = txtTitle.Text.Trim();

            //将查询条件放入Session中
            Session["QueryList"] = title + "@" + "BlogList";

        }

        private void LoadCondition()
        {
            string condition = Session["QueryList"] + "";

            string[] conditionAll = condition.Split('@');
            if (conditionAll.Length == 2)
            {
                string page = conditionAll[1];
                if (page == "BlogList")
                {
                    txtTitle.Text = conditionAll[0];
                }
            }
        }
        #endregion
    }
}